import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public class NGuessNumber {
    //generateAnswer方法 随机生成4个 0-9 的随机数
    public List<Integer> generateAnswer() {
        List<Integer> ranlist = new ArrayList<>();
        int max = 10, min = 0, a = 0;
        while (a < 4) {
            int b = 0;
            int ran = (int) (Math.random() * (max - min) + min);
            for (Integer integer : ranlist) {
                if (ran == integer) {
                    b = 1;
                    break;
                }
            }
            if (b == 0) {
                ranlist.add(ran);
                a++;
            }
        }
        return ranlist;
    }

    //用户输入方法
    public List<Integer> getPlayerInput() {
        List<Integer> ls = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 4; i++) {
            ls.add(sc.nextInt());//测试时换成  ls.add(1);
        }
        return ls;
    }

    //比较答案方法
    public List<Integer> compareGuessAnswer(List<Integer> comp, List<Integer> per) {
        List<Integer> da = new ArrayList<>();
        int a = 0;
        int b = 0;
        for (int i = 0; i < comp.size(); i++) {
            for (int j = 0; j < per.size(); j++) {
                if (Objects.equals(comp.get(i), per.get(j))) {
                    if (i == j) {
                        a++;
                    } else {
                        b++;
                    }
                }
            }
        }
        da.add(a);
        da.add(b);
        return da;
    }

    //判断游戏是否获胜方法
    public boolean isWin(int a) {
        return a == 4;
    }

    public static void main(String[] args) {
        NGuessNumber number = new NGuessNumber();
        Scanner sc = new Scanner(System.in);
        for (; ; ) {
            List<Integer> com = number.generateAnswer();
            System.out.println(com);
            System.out.println("请输入4个0-9的数字");
            for (; ; ) {
                List<Integer> per = number.getPlayerInput();
                List<Integer> da = number.compareGuessAnswer(com, per);
                boolean a = number.isWin(da.get(0));
                if (a) {
                    System.out.println("恭喜您获得游戏胜利");
                    break;
                } else {
                    System.out.println(da.get(0) + " A" + " " + da.get(1) + " B");
                }
            }
            System.out.println("结束游戏 N ，输入其他任意键开始下一局");
            String s = sc.next();

            if (s.equals("n") || s.equals("N")) {
                break;
            } else {
                System.out.println("输入错误");

            }
        }

    }
}